<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class App {
	
	private static $ci = null;
	
	public function __construct() {
		define('APPDIR', FCPATH.APPPATH);
		App::$ci =& get_instance();
		App::library('layout');
		App::helper('url');
		App::helper('app');
	}
	
	public static function library($name) {
		if (!isset(App::$ci->$name))
			App::$ci->load->library($name);
		return App::$ci->$name;
	}
	
	public static function view($tpl, $data) {
		App::$ci->load->view($tpl, $data);
	}
	
	public static function helper($name) {
		App::$ci->load->helper($name);
	}
	
	public static function model($name) {
		App::$ci->load->model($name);
		return App::$ci->$name;
	}
	
	public static function config($name) {
		return App::$ci->config->item($name);
	}

	public static function template($path) {
		return App::library('layout')->getTemplate($path);
	}
	
	public static function skinUrl($item) {
		return App::library('layout')->getSkinUrl($item);
	}
	
	public static function layout() {
		return App::library('layout');
	}
	
	public static function db($name=null) {
		$result = null;
		if ($name) {
			$result = App::$ci->load->database($name);
		} else {
			App::$ci->load->database();
			$result = App::$ci->db;
		}
		return $result;
	}
	
	public static function entity($name) {
		$path = capitalize($name);
		require_once(APPDIR.'entity/Core.php');
		require_once(APPDIR.'entity/'.$path.'.php');
		
		$className = 'Entity_'.str_replace('/', '_', $path);
		$result = new $className();
		return $result->setResource(App::db());
	}
	
}
